package com.jaks.xpense.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;

public class Globals {

	public static final String DatabaseName = "xpensedb";
	public static final int DatabaseVersion = 2;

	public static final String UpdateFromActivity = "updateFromActivity";
	public static final String Today = "today";
	public static final String Balance = "balance";

	public static final String SelectedTagIds = "SelectedTagIds";
	public static final String SelectedTagNames = "SelectedTagNames";

	public static final String TagForegroundColor = "#ffffff";
	public static final String TagBackgroundColor = "#353535";

	public static final String Empty = "";

	public static final String ExternalFolderName = "xpensedroid";

	private static final String SingleQuote = "'";
	private static final String DateFormatText = "yyyy-MM-dd";
	private static final SimpleDateFormat sdf = new SimpleDateFormat(DateFormatText);
	private static DecimalFormat decimalformat = new DecimalFormat("##,##,##,##0.00");

	public static String toDbString(String text) {
		return SingleQuote + text + SingleQuote;
	}

	public static String toDecimalText(double value) {
		return decimalformat.format(value);
	}

	public static Calendar toCalendar(String text) {
		Calendar calendar = Calendar.getInstance();
		try {
			Date date = sdf.parse(text);
			calendar.setTime(date);
		} catch (Exception ex) {
		}
		return calendar;
	}

	public static String toDateText(Calendar calendar) {
		try {
			return sdf.format(calendar.getTime());
		} catch (Exception ex) {
		}
		return Empty;
	}

	public static String toDateText(int year, int month, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DATE, day);

		return toDateText(calendar);
	}

	public static String toText(long[] values) {

		StringBuilder sb = new StringBuilder("");

		if (values != null)
			for (long value : values) {

				if (sb.length() > 0)
					sb.append(",");

				sb.append(String.valueOf(value));
			}

		return sb.toString();
	}

	public static long[] toLongArray(String text) {

		String[] ids = text.split(",");
		long[] array = new long[ids.length];
		int index = -1;

		for (String id : ids) {
			try {
				array[++index] = Long.parseLong(id);
			} catch (Exception ex) {
			}
		}

		return array;
	}

	public static String readTextFile(Context context, int resourceId) {
		InputStream inputStream = context.getResources().openRawResource(resourceId);
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
		BufferedReader reader = new BufferedReader(inputStreamReader);

		StringBuilder text = new StringBuilder();
		String line;
		try {
			while ((line = reader.readLine()) != null)
				text.append(line);
		} catch (IOException e) {
			return "";
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
			}
		}
		return text.toString();
	}

	/**
	 * Creates the specified <code>toFile</code> as a byte for byte copy of the
	 * <code>fromFile</code>. If <code>toFile</code> already exists, then it will
	 * be replaced with a copy of <code>fromFile</code>. The name and path of
	 * <code>toFile</code> will be that of <code>toFile</code>.<br/>
	 * <br/>
	 * <i> Note: <code>fromFile</code> and <code>toFile</code> will be closed by
	 * this function.</i>
	 * 
	 * @author Austyn Mahoney {@link http
	 *         ://stackoverflow.com/questions/6540906/android
	 *         -simple-export-import-of-sqlite-database/6542214#6542214}
	 * 
	 * @param fromFile
	 *          - FileInputStream for the file to copy from.
	 * @param toFile
	 *          - FileInputStream for the file to copy to.
	 */
	public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
		FileChannel fromChannel = null;
		FileChannel toChannel = null;
		try {
			fromChannel = fromFile.getChannel();
			toChannel = toFile.getChannel();
			fromChannel.transferTo(0, fromChannel.size(), toChannel);
		} finally {
			try {
				if (fromChannel != null) {
					fromChannel.close();
				}
			} finally {
				if (toChannel != null) {
					toChannel.close();
				}
			}
		}
	}
}
